package cn.tansanqinger.dao.sql.announcement;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IAnnouncementSqlDao;
import cn.tansanqinger.pojo.Announcement;
import cn.tansanqinger.util.PageData;

@Repository
public class AnnouncementSqlDaoImpl {
	
	@Autowired
	private IAnnouncementSqlDao dao;
	
	/**
	 * 查询数据
	 * @param keywords
	 * @param startTime
	 * @param endTime
	 * @param page
	 * @param limit
	 * @return
	 */
	public PageData selectAnnouncement(String keywords ,long startTime , long endTime, int page , int limit){
		PageData resultMap = new PageData();
		QueryWrapper<Announcement> queryWrapper = new QueryWrapper<Announcement>();
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("ano", keywords).or().like("atitle", keywords));
		}
		if(startTime!=-1&&endTime!=-1) {
			queryWrapper.lambda().gt(Announcement::getAtime, startTime).lt(Announcement::getAtime, endTime);
		}
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Announcement> pg = new Page<Announcement>(page,limit);
		IPage<Announcement> iPage = dao.selectPage(pg, queryWrapper);
		List<Announcement> coursesList = iPage.getRecords();
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	/**
	 * 获取编号
	 * @return
	 */
	public Integer serialNumber() {
		QueryWrapper<Announcement> queryWrapper = new QueryWrapper<Announcement>();
		queryWrapper.orderByDesc("asn").last("limit 1");
		Announcement object= dao.selectOne(queryWrapper);
		return object==null?10000001:object.getAsn()+1;
	}
	
	/**
	 * 添加信息
	 * @param course
	 * @return
	 */
	public int add(Announcement object) {
		return dao.insert(object);
	}
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Announcement selectOnt(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 查询信息
	 * @param csn
	 * @return
	 */
	public Announcement selectSn(int id) {
		QueryWrapper<Announcement> queryWrapper = new QueryWrapper<Announcement>();
		queryWrapper.lambda().eq(Announcement::getAsn, id);
		return dao.selectOne(queryWrapper);
	}
	
	/**
	 * 删除信息
	 * @param cid
	 * @return
	 */
	public int deleteOne(int id) {
		return dao.deleteById(id);
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Announcement object) {
		return dao.updateById(object);
	}

}
